package com.cxh.rmsq.ui.fragment.self

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import com.cxh.rmsq.R
import com.cxh.rmsq.common.Constant
import com.cxh.rmsq.net.newNet.ProjectViewModel
import com.cxh.rmsq.ui.activity.self.AuthenticationActivity
import com.cxh.rmsq.ui.activity.self.FansActivity
import com.cxh.rmsq.ui.activity.self.LookMeActivity
import io.rong.callkit.util.SPUtils
import io.rong.imkit.conversationlist.ConversationListFragment
import io.rong.imkit.widget.adapter.ViewHolder


/**
 * 会话列表
 */
class MessageListFragment : ConversationListFragment(), View.OnClickListener {

    private lateinit var rl_zan: RelativeLayout
    private lateinit var rl_comment: RelativeLayout
    private lateinit var rl_focus: RelativeLayout
    private lateinit var rl1: RelativeLayout
    private lateinit var rl2: RelativeLayout
    private lateinit var rl3: RelativeLayout
    private lateinit var stv_zan: TextView
    private lateinit var stv_comment: TextView
    private lateinit var stv_focus: TextView
    private val viewModel by lazy {
        ViewModelProvider.NewInstanceFactory().create(ProjectViewModel::class.java)
    }

    companion object {
        fun newInstance() = MessageListFragment()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_message_list, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

//        view.findViewById<LinearLayout>(R.id.cl_toolbar).setPadding(0, BarUtils.getStatusBarHeight(), 0, 0)
//        //        findViewById<ConstraintLayout>(R.id.cl_toolbar).setPadding(0, BarUtils.getStatusBarHeight(), 0, 0)
//        BarUtils.setStatusBarColor(requireActivity(), ColorUtils.getColor(R.color.transparent))
//        BarUtils.setStatusBarLightMode(requireActivity(), true)

        rl_zan = view.findViewById<RelativeLayout>(R.id.rl_zan)
        rl_comment = view.findViewById<RelativeLayout>(R.id.rl_comment)
        rl_focus = view.findViewById<RelativeLayout>(R.id.rl_focus)
        rl1 = view.findViewById<RelativeLayout>(R.id.rl1)
        rl2 = view.findViewById<RelativeLayout>(R.id.rl2)
        rl3 = view.findViewById<RelativeLayout>(R.id.rl3)
        stv_zan = view.findViewById<TextView>(R.id.stv_zan)
        stv_comment = view.findViewById<TextView>(R.id.stv_comment)
        stv_focus = view.findViewById<TextView>(R.id.stv_focus)
        rl_zan.setOnClickListener(this)
        rl_comment.setOnClickListener(this)
        rl_focus.setOnClickListener(this)
    }

    override fun onItemClick(view: View?, holder: ViewHolder?, position: Int) {

        if ("2" != SPUtils.get(activity, Constant.AuthenticationStatus, "")) {
            //已通过
            val intent = Intent(
                activity,
                AuthenticationActivity::class.java
            )
            startActivity(intent)
            Toast.makeText(activity, "请先进行实名认证", Toast.LENGTH_SHORT).show()
            return
        }
        super.onItemClick(view, holder, position)
    }

    override fun onResume() {
        super.onResume()
        viewModel.getNoReadCount {
            if (it.code == 200) {
                stv_zan.text = it.data.likedNoReadCount.toString()
                stv_comment.text = it.data.commentNoReadCount.toString()
                stv_focus.text = it.data.followerNoReadCount.toString()

                rl1.isVisible = it.data.likedNoReadCount > 0
                rl2.isVisible = it.data.commentNoReadCount > 0
                rl3.isVisible = it.data.followerNoReadCount > 0
            }
        }
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.rl_zan -> {
                startActivity(
                    Intent(activity, LookMeActivity::class.java)
                        .putExtra("isLookMe", 3)
                )
            }

            R.id.rl_comment -> {
                startActivity(
                    Intent(activity, LookMeActivity::class.java)
                        .putExtra("isLookMe", 4)
                )
            }

            R.id.rl_focus -> {
                startActivity(
                    Intent(activity, FansActivity::class.java)
                        .putExtra("isFans", true)
                )
            }
        }

    }
}